class Solution {
public:
    int f(string s)
    {
        sort(s.begin(),s.end());
        int cnt=1;
        for(int i=1;i<s.size();++i)
        {
            if(s[i]==s[0]) ++cnt;
            else break;
        }
        return cnt;
    }

    vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
        vector<int> nums;
        
        for(auto &w:words)
            nums.push_back(f(w));
        sort(nums.begin(),nums.end());
        vector<int> ret;
        for(auto &q:queries)
        {
            int qf=f(q);
            int left=0,right=words.size();
            while(left<right)
            {
                int mid=(left+right)>>1;
                if(nums[mid]<=qf) left=mid+1;
                else right=mid;
            }
            ret.push_back(nums.size()-left);
        }
        return ret;
    }
};
